package com.example.binpacking.util;

import java.lang.reflect.Field;
import java.util.Collection;

public class NullToEmptyStringUtil {

    // Method to update null fields in a collection of objects
    public static <T> void replaceNullsWithEmptyString(Collection<T> collection) {
        for (T item : collection) {
            if (item != null) {
                replaceNullsWithEmptyString(item);
            }
        }
    }

    // Method to update null fields in a single object
    private static void replaceNullsWithEmptyString(Object obj) {
        Field[] fields = obj.getClass().getDeclaredFields();
        for (Field field : fields) {
            if (field.getType().equals(String.class)) {
                field.setAccessible(true);
                try {
                    if (field.get(obj) == null) {
                        field.set(obj, "");
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
